// src/stores/modules/member.ts

import type {BaseUserData, LoginRequestData} from '@/api/login/types/login'
import {defineStore} from 'pinia'
import {ref} from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
    'member',
    () => {
        // 会员信息
        const profile = ref<string>()

        const userInfo = ref<BaseUserData>({})

        const getUserInfo = () => {
            return userInfo.value
        }
        const setUserInfo = (val: BaseUserData) => {
            userInfo.value = val
        }

        // 保存会员信息，登录时使用
        const setProfile = (val: string) => {
            profile.value = val
        }
        const getProfile = () => {
            return profile.value
        }

        // 清理会员信息，退出时使用
        const clearProfile = () => {
            profile.value = undefined
            userInfo.value = {}
        }

        // 记得 return
        return {userInfo, profile, setProfile, clearProfile, getProfile, getUserInfo, setUserInfo}
    },
    {
        // 小程序端配置
        persist: {
            storage: {
                getItem(key: string) {
                    return uni.getStorageSync(key)
                },
                setItem(key: string, value: string) {
                    uni.setStorageSync(key, value)
                },
            },
        },
    },
)
